package guo.listen;

import guo.entity.Student;
import guo.entity.User;
import guo.event.StudentEvent;
import guo.event.UserEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author: gsq
 * @description: TODO
 * @date: 2024/6/5 9:28
 * @version: 1.0
 */
@Component
public class StudentEventListen {

    @Autowired
    private ApplicationEventPublisher publisher;

    @Async
    @EventListener(StudentEvent.class)
    public void studentEvent(StudentEvent event) throws InterruptedException {
        Student student = event.getStudent();
        System.out.println("监听到了student信息 "+student);
        Thread.sleep(10000);
        System.out.println("student睡眠结束");

        User user = new User();
        user.setId(2);
        user.setName("nihao");
        user.setPhone("sada");
        publisher.publishEvent(new UserEvent(user));
    }
}
